package com.seed.uaa.tenant.web;

import com.seed.core.annotation.Declares;
import com.seed.core.annotation.EntityClass;
import com.seed.core.pojo.R;
import com.seed.core.pojo.SetDTO;
import com.seed.core.pojo.StatPage;
import com.seed.core.annotation.SeedDeclare;
import com.seed.uaa.pojo.OwnerAction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "租户管理")
@RestController
@RequestMapping("tenant")
@EntityClass(Tenant.class)
public class TenantController {

    private final TenantService tenantService;

    public TenantController(TenantService userService) {
        this.tenantService = userService;
    }

    @Operation(summary = "分页列表")
    @GetMapping("page")
    @Declares("id,organization,name,logo,tenantname,expire_date,usable,loginable,enable,active")
    public R<StatPage<Tenant>> page(Integer pageNumber, Integer pageSize) {
        return R.data(tenantService.page(pageNumber, pageSize));
    }

    @Operation(summary = "查找带回")
    @GetMapping("lookup")
    public R<StatPage<Tenant>> lookup(Integer pageNumber, Integer pageSize) {
        return R.data(tenantService.page(pageNumber, pageSize));
    }

    @Operation(summary = "保存")
    @PostMapping("save")
    public R<Boolean> save(@RequestBody Tenant tenant) {
        return R.data(tenantService.save(tenant));
    }

    @Operation(summary = "更新")
    @GetMapping("update/{id}")
    @Declares("name, remark")
    public R<Tenant> update(@PathVariable String id) {
        return R.data(tenantService.detail(id));
    }

    @Operation(summary = "更新")
    @PutMapping("update")
    public R<Boolean> update(@RequestBody Tenant tenant) {
        return R.data(tenantService.update(tenant));
    }

    @Operation(summary = "设置LOGO")
    @GetMapping("logo/{id}")
    public R<?> logo(@PathVariable String id) {
        return R.data(tenantService.get(id));
    }

    @Operation(summary = "设置LOGO")
    @PutMapping("logo")
    public R<Boolean> logo(@RequestBody SetDTO dto) {
        return R.data(tenantService.set(dto));
    }

    @Operation(summary = "设置账号")
    @GetMapping("tenantname/{id}")
    public R<?> tenantname(@PathVariable String id) {
        return R.data(tenantService.get(id));
    }

    @Operation(summary = "设置账号")
    @PutMapping("tenantname")
    public R<Boolean> tenantname(@RequestBody SetDTO dto) {
        return R.data(tenantService.set(dto));
    }

    @Operation(summary = "重置密码")
    @PutMapping("reset")
    public R<Boolean> reset(String id) {
        return R.data(tenantService.resetPassword(id));
    }

    @Operation(summary = "设置截止日期")
    @GetMapping("expireDate/{id}")
    public R<?> expireDate(@PathVariable String id) {
        return R.data(tenantService.get(id));
    }

    @Operation(summary = "设置截止日期")
    @PutMapping("expireDate")
    public R<Boolean> expireDate(@RequestBody SetDTO dto) {
        return R.data(tenantService.set(dto));
    }

    @Operation(summary = "设置启用")
    @GetMapping("usable/{id}")
    @Declares("id, usable, loginable, enable, reason")
    public R<Tenant> usable(@PathVariable String id) {
        return R.data(tenantService.detail(id));
    }

    @Operation(summary = "设置启用")
    @PutMapping("usable")
    @Declares("usable, loginable, enable, reason")
    public R<Boolean> usable(@RequestBody Tenant tenant) {
        return R.data(tenantService.update(tenant));
    }

    @Operation(summary = "激活账户")
    @PutMapping("active")
    @SeedDeclare(declare = "active", value = "yes")
    public R<Boolean> active(@RequestBody SetDTO dto) {
        return R.data(tenantService.set(dto));
    }

    @Operation(summary = "获取角色")
    @GetMapping("roles")
    public R<?> roles(String id) {
        return R.data(tenantService.get(id));
    }

    @Operation(summary = "分配角色")
    @PutMapping("roles")
    public R<Boolean> roles(SetDTO dto) {
        return R.data(tenantService.set(dto));
    }

    @Operation(summary = "查看功能权限")
    @GetMapping("getActions/{id}")
    public R<OwnerAction> getActions(@PathVariable String id) {
        return R.data(tenantService.getTenantAction(id));
    }

    @Operation(summary = "获取功能权限")
    @GetMapping("actions/{id}")
    public R<List<String>> actions(@PathVariable String id) {
        return R.data(tenantService.getActions(id));
    }

    @Operation(summary = "分配功能权限")
    @PutMapping("actions")
    public R<Boolean> actions(@RequestBody SetDTO dto) {
        return R.data(tenantService.setActions(dto));
    }

    @Operation(summary = "详情")
    @GetMapping("detail/{id}")
    public R<Tenant> detail(@PathVariable String id) {
        return R.data(tenantService.detail(id));
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public R<Boolean> remove(@PathVariable String id) {
        return R.data(tenantService.remove(id));
    }
}
